#include "widget.h"
#include "ui_widget.h"
#include <QFileDialog>
#include <QDebug>
#include <iostream>
#include <QMessageBox>
#include <QtXlsx/xlsxdocument.h>
#include <QtXlsx/xlsxformat.h>

using namespace std;

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
}


Widget::~Widget()
{
    delete ui;
}

void Widget::alert(QString msg)
{
    QMessageBox::information(this, "", msg);
}



void Widget::doExcel(QString fileName)
{
    QXlsx::Document xlsx;
    QXlsx::Format format;


    xlsx.addSheet();
    QFile file(fileName);
    if(!file.open(QIODevice::ReadOnly ))
    {
        QMessageBox::information(this, tr("打开文件错"), tr("打开文件失败"));
    }
    char buff[2048] = {};
    int cnt = 0;
    while(file.readLine(buff, 2048)>0)
    {
        QString line = QString::fromLocal8Bit(buff).trimmed();
        cnt++;
        if(cnt == 1)
        {
            format.setFontSize(12);
            format.setHorizontalAlignment(QXlsx::Format::AlignHCenter);
            format.setBorderStyle(QXlsx::Format::BorderThin);

            xlsx.write("G1", line, format);
        }
        if(cnt == 3)
        {
            QStringList temp = line.split(' ');
            xlsx.write("A3", temp[0].trimmed(), format);
            xlsx.write("B3", temp[1].trimmed(), format);
            xlsx.write("I3", temp[105].trimmed(), format);
         }
        if(cnt == 5)
        {


            QStringList temp = line.split(" ");

            //机构代码

            xlsx.write("A4", temp[0].mid(1, 8), format);
            xlsx.write("B4", temp[10], format);
            xlsx.write("D4", temp[32], format);
            xlsx.write("F4", temp[53], format);
            xlsx.write("I4", temp[88], format);
            xlsx.write("K4", temp[113], format);
            xlsx.write("M4", temp[129], format);
            xlsx.write("O4", temp[145], format);
            xlsx.write("P4", temp[153], format);
            xlsx.write("Q4", temp[163], format);
            qDebug()<<line<<endl;
        }

        if(cnt == 7)
        {
            QStringList temp = line.split(" ");
            xlsx.write("B5", temp[15], format);
            xlsx.write("C5", temp[25], format);
            xlsx.write("D5", temp[37], format);
            xlsx.write("E5", temp[49], format);
            xlsx.write("F5", temp[62], format);
            xlsx.write("G5", temp[74], format);
            xlsx.write("H5", temp[93], format);
//            xlsx.write("I5", temp[113], format);
            xlsx.write("J5", temp[113], format);
            xlsx.write("K5", temp[129], format);
            xlsx.write("L5", temp[140], format);
            xlsx.write("M5", temp[152], format);
            xlsx.write("N5", temp[163], format);



        }
        if(cnt == 9)
        {
            QStringList temp = line.split(" ");
            xlsx.write("H6", temp[106].mid(1, 8), format);
            xlsx.write("I6", temp[114].mid(1, 8), format);

        }

        if(cnt == 11)
        {
            alert(QString::fromLocal8Bit(buff));

            cout<<"i heare ----------"<<endl;

        }
        cout<<buff<<endl;
        cout<<"-------------"<<endl;
        memset(buff, 0, strlen(buff)+1);
    }
    file.close();
    xlsx.saveAs("e:/a.xlsx");

}

void Widget::on_exportButton_clicked()
{
     QStringList files = QFileDialog::getOpenFileNames(this, tr("选择文本文件"),"C:/Users/Administrator/Desktop", "Text files(*.txt)");
     qDebug()<<files<<endl;
     QStringList temp = files[0].split('/');
     alert(temp[temp.size()-1]);

//     for(int i = 0; i<files.size(); i++)
//     {
         doExcel(files.at(0));
//     }

}

void Widget::on_exitButton_clicked()
{
    close();
}
